#include "StdAfx.h"
#include "GTDTreeCtrl.h"
#include <algorithm>
#include "TreeCtrlItem.h"


IMPLEMENT_DYNAMIC(CGTDTreeCtrl, CTreeCtrl)	

BEGIN_MESSAGE_MAP(CGTDTreeCtrl, CTreeCtrl)
	ON_NOTIFY_REFLECT(NM_CLICK, OnClick)
	//ON_NOTIFY_REFLECT(NM_DBLCLK, OnDblclk)
	ON_WM_LBUTTONDBLCLK()
	//ON_WM_ERASEBKGND()	
	//ON_WM_PAINT()
	ON_NOTIFY_REFLECT(NM_RCLICK, OnRclick)	
	ON_NOTIFY_REFLECT(TVN_ITEMEXPANDING, OnTvnItemexpanding)
	ON_NOTIFY_REFLECT(TVN_SELCHANGED, OnTvnSelchanged)
	//ON_NOTIFY_REFLECT(TVN_ENDLABELEDIT, OnTvnEndlabeledit)
	//ON_NOTIFY_REFLECT(TVN_BEGINLABELEDIT, OnTvnBeginlabeledit)
END_MESSAGE_MAP()



void CGTDTreeCtrl::SetItemImpl( HTREEITEM item, TreeItemImpl*pImpl )
{
	if(pImpl )
	{
		m_vItemImpls.addData(pImpl);
	}
	SetItemData(item,(DWORD_PTR)pImpl);
	
}

TreeItemImpl* CGTDTreeCtrl::GetItemImpl( HTREEITEM item ) const
{
	TreeItemImpl* pImpl = (TreeItemImpl*)GetItemData(item);
	if(m_vItemImpls.hasData(pImpl))
		return pImpl;
	return NULL;
}

void CGTDTreeCtrl::OnClick( NMHDR* pNMHDR, LRESULT* pResult )
{
	//if(pNMHDR)
		//NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	CPoint pt;
	CRect rt;
	GetCursorPos(&pt);
	ScreenToClient(&pt);
	UINT flag;
	HTREEITEM hItem = HitTest(pt, &flag);
	if (hItem == NULL)
	{
		return;
	}

	TreeItemImpl* pItemImpl = GetItemImpl(hItem);
	if(pItemImpl)
	{
		pItemImpl->OnClick( CTreeCtrlItem(this,hItem) );
	}
}



void CGTDTreeCtrl::OnLButtonDblClk( UINT nFlags, CPoint point )
{
	UINT flag;
	HTREEITEM hItem = HitTest(point, &flag);
	if (hItem == NULL)
	{
		return;
	}

	TreeItemImpl* pItemImpl = GetItemImpl(hItem);
	if(pItemImpl)
	{
		pItemImpl->OnDblClick( CTreeCtrlItem(this,hItem) );
	}
}

void CGTDTreeCtrl::OnRclick( NMHDR* pNMHDR, LRESULT* pResult )
{
	CPoint pt;
	CRect rt;
	GetCursorPos(&pt);
	ScreenToClient(&pt);
	UINT flag;
	HTREEITEM hItem = HitTest(pt, &flag);
	if (hItem == NULL)
	{
		return;
	}

	TreeItemImpl* pItemImpl = GetItemImpl(hItem);
	if(pItemImpl)
	{
		pItemImpl->OnRClick( CTreeCtrlItem(this,hItem) );
	}
}

void CGTDTreeCtrl::OnTvnItemexpanding( NMHDR *pNMHDR, LRESULT *pResult )
{

}

void CGTDTreeCtrl::OnTvnSelchanged( NMHDR *pNMHDR, LRESULT *pResult )
{

}

void CGTDTreeCtrl::Init()
{
	/*SetExtendedStyle(LVS_EX_AUTOSIZECOLUMNS | LVS_EX_FULLROWSELECT |
		LVS_EX_LABELTIP | LVS_EX_DOUBLEBUFFER | LVS_EX_AUTOSIZECOLUMNS);*/

	SetExtendedStyle(TVS_EX_FADEINOUTEXPANDOS | TVS_EX_DOUBLEBUFFER | TVS_EX_RICHTOOLTIP,
		TVS_EX_FADEINOUTEXPANDOS | TVS_EX_DOUBLEBUFFER | TVS_EX_RICHTOOLTIP);
}
